﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace RclSystem
{
    public partial class DeliveryDialog : UIEditForm
    {
        public DeliveryDialog()
        {
            InitializeComponent();
            DataTable dt_saleman = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_saleman");
            cb_saleman.DataSource = dt_saleman;
            cb_saleman.DisplayMember = "name";
            cb_saleman.ValueMember = "id";
            cb_saleman.SelectedIndex = -1;
            dt_deliver.Value = DateTime.Now;
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cb_company, "请输入单位名称") && CheckEmpty(cb_saleman, "请选择业务员") && CheckEmpty(dt_deliver, "请选择发货日期");
        }

        private DeliveryEntity delivery;

        public DeliveryEntity DeliveryEntity
        {
            get
            {
                if (delivery == null)
                {
                    delivery = new DeliveryEntity();
                }

                delivery.salesman = cb_saleman.Text;
                delivery.companyname = cb_company.Text;
                delivery.deliverdate = dt_deliver.Value;
                delivery.salecount = double.Parse(txt_salecount.Text);
                delivery.saleamount = decimal.Parse(txt_saleamount.Text);
                delivery.perton = double.Parse(txt_perton.Text);
                delivery.bsalary = double.Parse(txt_bsalary.Text);
                delivery.accountdays = int.Parse(txt_accountdays.Text);
                delivery.duedate = dt_duedate.Value;
                delivery.settlement = double.Parse(txt_settlement.Text);
                delivery.interestsubsidy = double.Parse(txt_interestsubsidy.Text);
                delivery.tcamount = double.Parse(txt_tcamount.Text);
                return delivery;
            }

            set
            {
                delivery = value;
                cb_saleman.Text = value.salesman;
                cb_company.Text = value.companyname;
                dt_deliver.Value = value.deliverdate;
                txt_salecount.Text = value.salecount.ToString();
                txt_saleamount.Text = value.saleamount.ToString();
                txt_perton.Text = value.perton.ToString();
                txt_bsalary.Text = value.bsalary.ToString();
                txt_accountdays.Text = value.accountdays.ToString();
                dt_duedate.Value = value.duedate;
                txt_settlement.Text = value.settlement.ToString();
                txt_interestsubsidy.Text = value.interestsubsidy.ToString();
                txt_tcamount.Text = value.tcamount.ToString();
            }
        }

        private void cb_saleman_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cb_saleman.SelectedIndex != -1)
            {
                if(cb_saleman.SelectedValue.GetType() != typeof(DataRowView))
                {
                    DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where parentid = " + cb_saleman.SelectedValue.ToString());
                    cb_company.DataSource = dt_company;
                    cb_company.DisplayMember = "name";
                    cb_company.ValueMember = "id";              
                }
                
            }            
        }

        private void txt_accountdays_TextChanged(object sender, EventArgs e)
        {
            if(dt_deliver.Value != null)
            {
                DateTime odt = dt_deliver.Value;
                DateTime ndt = odt.AddDays(int.Parse(txt_accountdays.Text));
                dt_duedate.Value = ndt;
            }
        }

        private void txt_perton_TextChanged(object sender, EventArgs e)
        {
            double pcount = 0;
            double perton = 0;
            if(!double.TryParse(txt_salecount.Text, out pcount))
            {
                this.ShowErrorDialog2("异常的销售数量数据:" + txt_salecount.Text);
                return;
            }
            if (!double.TryParse(txt_perton.Text, out perton))
            {
                this.ShowErrorDialog2("异常的元/吨数据:" + txt_perton.Text);
            }
            txt_bsalary.Text = Math.Round(pcount * perton, 2).ToString();
        }

        private void txt_salecount_TextChanged(object sender, EventArgs e)
        {
            double pcount = 0;
            double perton = 0;
            if (!double.TryParse(txt_salecount.Text, out pcount))
            {
                this.ShowErrorDialog2("异常的销售数量数据:" + txt_salecount.Text);
                return;
            }
            if (!double.TryParse(txt_perton.Text, out perton))
            {
                this.ShowErrorDialog2("异常的元/吨数据:" + txt_perton.Text);
            }
            txt_bsalary.Text = Math.Round(pcount * perton, 2).ToString();
        }
    }
}
